//
//  CADetailController.m
//  cascade-realm
//
//  Created by QSC on 30/5/24.
//

#import "CADetailController.h"
#import "CAImageBrowser.h"
#import "ImageCell.h"


@interface CADetailController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView * collectionView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, assign) double collectionH;
@property (nonatomic, strong) UIButton * likeBtn;
@property (nonatomic, strong) UIButton * TranslateBtn;
@property (nonatomic, strong) UILabel * TranslateLabel;
@property (nonatomic, strong) CAPageControl * PageControl;
@property (nonatomic, strong) UIImageView * addImageV;
@end

@implementation CADetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(IsX){
        self.collectionH = ScreenW * 473/375;
    }else{
        self.collectionH = ScreenW ;
    }
    
   
    [self setUpNavView];
    
    [self setUpCollection];
    
    [self setBottomView];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AutoTranslate"]) {
        [self TranslateDidClick];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(followChange) name:FollowDidChange object:nil];
}

- (void)setUpNavView{
    
    [self.navView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(90);
    }];
    
    [self.backBtn setImage:Img(@"back2") forState:UIControlStateNormal];
    
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.navView).offset(KNavigationStatusHeight * 0.5);
        make.left.equalTo(self.navView).offset(20);
        make.size.mas_equalTo(28);
    }];
    
    
    UIImageView * headImagev = [UIImageView new];
    headImagev.layer.borderWidth = 1;
    headImagev.layer.borderColor = [UIColor whiteColor].CGColor;
    headImagev.layer.cornerRadius = 14;
    headImagev.clipsToBounds = YES;
    [headImagev sd_setImageWithURL:[NSURL URLWithString:self.model.getUser.avatar] placeholderImage:Img(@"defult_head")];
    
    UIImageView * addImgv = [[UIImageView alloc] initWithImage:Img(@"add_home")];
    self.addImageV = addImgv;
    addImgv.hidden = [CAUserInfo.shared.followIds containsObject:self.model.binduserId];
    
    WeakSelf
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [Tool Follow:weakSelf.model.binduserId];
    }];
    [headImagev addGestureRecognizer:tap];
    headImagev.userInteractionEnabled = YES;
    
    
    
    [self.navView addSubview:headImagev];
    [headImagev mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.backBtn);
        make.left.equalTo(self.backBtn.mas_right).offset(12);
        make.size.mas_equalTo(28);
    }];
    
    [self.navView addSubview:addImgv];
    [addImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(headImagev);
        
    }];
    
    UIButton * moreBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    [moreBtn setImage:Img(@"detail_more") forState:UIControlStateNormal];
    [self.navView addSubview:moreBtn];
    [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headImagev);
        make.right.equalTo(self.navView).offset(-20);
        make.size.mas_equalTo(28);
    }];
    [moreBtn addTarget:self action:@selector(moreClick) forControlEvents:UIControlEventTouchUpInside];
    
    if ([self.model.binduserId isEqualToString:CAUserInfo.shared.userInfo.userId]) {
        moreBtn.hidden = YES;
        addImgv.hidden = YES;
        headImagev.userInteractionEnabled = NO;
    }
    
    
    
    self.titleLabel.text = self.model.getUser.nickname;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = Font(14);
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headImagev);
        make.left.equalTo(headImagev.mas_right).offset(12);
        make.right.equalTo(moreBtn.mas_left).offset(0);
    }];
    
    UIImageView * bgImgv = [UIImageView new];
    bgImgv.image = Img(self.model.cover);
    // 毛玻璃效果的style
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
    // 毛玻璃视图
    UIVisualEffectView *visualView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    // addSubview
    [bgImgv addSubview:visualView];
    [visualView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(bgImgv);
    }];
    
    [self.navView insertSubview:bgImgv atIndex:0];
    [bgImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.navView);
    }];
    
}

- (void)followChange{
    self.addImageV.hidden = [CAUserInfo.shared.followIds containsObject:self.model.binduserId];
}

- (void)setUpCollection{
    self.scrollView = [UIScrollView new];
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.navView.mas_bottom);
    }];
    
    
    self.contentView = [UIView new];

    [self.scrollView addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.width.bottom.equalTo(self.scrollView);
    }];
    
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(-6);
        make.height.mas_equalTo(self.collectionH);
   
    }];
    self.collectionView.layer.cornerRadius = 12;
    self.collectionView.clipsToBounds = YES;
    
    
    CAPageControl * PageControl = [CAPageControl new];
    PageControl.numberOfPages = self.model.allImages.count;
    self.PageControl = PageControl;
   
    
    [self.contentView addSubview:PageControl];
   
    [PageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.collectionView).inset(12);
        make.right.equalTo(self.collectionView).offset(-12);
        make.width.mas_equalTo(self.model.allImages.count * 8 + 20);
        make.height.mas_equalTo(12);
    }];
}

- (void)setBottomView{
    
    UIButton * likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [likeBtn setBackgroundImage:Img(@"detail_unlike") forState:UIControlStateNormal];
    [likeBtn setBackgroundImage:Img(@"detail_like") forState:UIControlStateSelected];
    
    [self.contentView addSubview:likeBtn];
    [likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.collectionView.mas_bottom).offset(-40);
        make.size.mas_equalTo(80);
    }];
    [likeBtn addTarget:self action:@selector(likeClick) forControlEvents:UIControlEventTouchUpInside];

    self.likeBtn = likeBtn;
    self.likeBtn.selected = self.model.islike;
    
    UILabel * titleLabel = [UILabel LabelWithFont:BoldFont(16) color:Hex(0x202020) text:self.model.title];
    titleLabel.numberOfLines = 0;
    [self.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(20);
        make.top.equalTo(likeBtn.mas_bottom).offset(16);
    }];
    
    UILabel * contentLabel = [UILabel LabelWithFont:Font(12) color:Hex(0x565656) text:self.model.content];
    contentLabel.numberOfLines = 0;
    [self.contentView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(20);
        make.top.equalTo(titleLabel.mas_bottom).offset(16);
    }];
    
    UIButton *TranslateBtn = [UIButton ButtonWithFont:Font(11) color:Hex(0xC8EE4C) text:@"Translate"];
    TranslateBtn.backgroundColor = Hex(0x1A3710);
    TranslateBtn.layer.cornerRadius = 4;
    [TranslateBtn setImage:Img(@"Translate") forState:UIControlStateNormal];
    TranslateBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 10, 3, 10);
    [TranslateBtn sizeToFit];
    [TranslateBtn setButtonImageTitleStyle:ButtonImageTitleStyleLeft padding:4];
    
    [self.contentView addSubview:TranslateBtn];
    [TranslateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-20);
        make.top.equalTo(contentLabel.mas_bottom).offset(27);
        
    }];
    self.TranslateBtn = TranslateBtn;
    [TranslateBtn addTarget:self action:@selector(TranslateDidClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel * TranslateLabel = [UILabel LabelWithFont:Font(12) color:Hex(0x565656) text:@""];
    self.TranslateLabel = TranslateLabel;
    
    [self.contentView addSubview:TranslateLabel];
    [TranslateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(contentLabel);
        make.top.equalTo(contentLabel.mas_bottom).offset(27);
        make.bottom.equalTo(self.contentView).offset(-80);
    }];
    TranslateLabel.numberOfLines = 0;
    TranslateLabel.hidden = true;
    
}



- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        
        layout.itemSize = CGSizeMake(ScreenW, self.collectionH);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
 
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
        
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        
        [collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"ImageCell"];
        // 去掉滚动条
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.pagingEnabled = YES;
        collectionView.bounces = NO;
        _collectionView = collectionView;
    }
    return  _collectionView;;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.model.allImages.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *) collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
    cell.imageV.image = Img(self.model.allImages[indexPath.row]);
  
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [CAImageBrowser ShowWithImages:self.model.allImages index:indexPath.item];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  
    self.PageControl.currentPage = scrollView.contentOffset.x / ScreenW;
 
    
}



- (void)likeClick{
    
    [Tool ShowLoading];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [Tool HideLoading];
      
        [self.model SetIslikeInDB:!self.model.islike];

        self.likeBtn.selected = self.model.islike;
   
    });
    
   
    
}

- (void)TranslateDidClick{
    
    
    [Http getTranslate:self.model.content sucess:^(NSString * _Nonnull str) {
       
        self.TranslateLabel.text = str;
        self.TranslateLabel.hidden = NO;
        self.TranslateBtn.hidden = YES;
    }];
}



- (void)moreClick{
    
  
    [Tool moreClick:self.model.binduserId];
}



@end





